<?php
class Tourism_provider_category_model extends BZ_Model
{
    protected $table    = 'tourism_provider_category';
 
     /**
     * 总数
     * @param unknown $param
     */
    public function total($param = array())
    {
        if(!empty($param['category_name'])){
            $this->db->like('category_name',$param['category_name']);
        }
        if(!empty($param['uid'])){
            $this->db->like('provider_uid',$param['uid']);
        }
        return $this->db->count_all_results($this->table);
    }

     /**
     * 列表
     * @param unknown $page_num
     * @param unknown $num
     * @param unknown $param
     */
    public function page_list($page_num, $num, $param = array())
    {
        if(!empty($param['category_name'])){
            $this->db->like('category_name',$param['category_name']);
        }
        if(!empty($param['uid'])){
            $this->db->like('provider_uid',$param['uid']);
        }
        $this->db->limit($page_num,$num);
        $this->db->order_by('is_main','desc');
        $this->db->order_by('sort','desc');
        return $this->db->get($this->table);
    }
    
     /**
     * 插入
     * @param unknown $param
     */
    public function insertProviderCategory($param = array()){
    	
        $data = array(
        	   'provider_uid'  => $param['uid'],
               'category_name' => $param['category_name'],
               'is_main'       => '1',  // 默认不是主类别
               'sort'          => $param['sort'],
               'creat_at'      => date('Y-m-d H:i:s')
        );
        return $this->db->insert($this->table,$data);
    }
    
     /**
     * 根据id查询
     * @param unknown $id
     */
    public function findProviderById($id){
    	
        $this->db->where('id',$id);
        return $this->db->get($this->table);
    }
    
     /**
     *更新供应商的类别
     *@param unknown $param
     */
    public function updateProviderCategory($id,$param = array()){
    	
        $data = array(
            'category_name' => $param['category_name'],
            'sort'          => $param['sort'],
        );
        $this->db->where('id',$id);
        return $this->db->update($this->table,$data);
    }
    
    /**
     * 更新类别是不是主类别
     * @param unknown $id
     * @param unknown $flag
     */
    public function updateProviderCategoryMain($id,$uid){
    	
        $this->db->where('provider_uid',$uid);
        $this->db->where('is_main',2);
        $this->db->update($this->table,array('is_main'=>1)); // 先把所有的更新为不是默认
        
        $this->db->where('id',$id);
        return $this->db->update($this->table,array('is_main'=>2));
    }
    
     /**
     * 根据类别搜索
     * @param unknown $category_name
     */
    public function providerCategoryByName($category_name,$provider_uid){
    	
        $this->db->where('category_name',$category_name);
        $this->db->where('provider_uid',$provider_uid);
        return $this->db->get($this->table);
    }
    
     /**
     * 删除
     * @param unknown $id
     */
    public function deleteProviderCategory($id){
    	
        $this->db->where('id',$id);
        return $this->db->delete($this->table);
    }
    
     /**
     * 根据uid 获取
     * @param unknown $uid
     */
    public function providerCategoryByUid($uid){
    	
        $this->db->where('provider_uid',$uid);
        $this->db->where('is_on_sale','1');
        return $this->db->get($this->table);
    }
    
    /**
     * 
     * @param unknown $id
     * @param unknown $state
     */
    public function  upOrDown($id,$state){
    	
        $data = array(
        	 'is_on_sale' => $state
        );
        $this->db->where('id',$id);
        return $this->db->update($this->table,$data);
    }
}